// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EncodePasswordUser.cs" company="Donga CS">
//   2011
// </copyright>
// <summary>
//   The EncodePasswordUser.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DONGACS.Common
{
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// The session details.
    /// </summary>
    public class EncodePasswordUser
    {
        /// <summary>
        /// Encode Password
        /// </summary>
        /// <param name="originalPassword">
        /// The original password.
        /// </param>
        /// <returns>
        /// string value.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">Thrown when </exception>
        /// <exception cref="System.Text.EncoderFallbackException">Thrown when </exception>
        /// <exception cref="System.ObjectDisposedException">Thrown when </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown when </exception>
        /// <exception cref="System.ArgumentException">Thrown when </exception>
        /// <exception cref="System.FormatException">Thrown when </exception>
        public static string EncodePassword(string originalPassword)
        {
            //// convert to Bytes
            Encoder enc = System.Text.Encoding.Unicode.GetEncoder();

            byte[] unicodeText = new byte[originalPassword.Length * 2];

            enc.GetBytes(originalPassword.ToCharArray(), 0, originalPassword.Length, unicodeText, 0, true);

            MD5 md5 = new MD5CryptoServiceProvider();

            byte[] result = md5.ComputeHash(unicodeText);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X2"));
            }

            return sb.ToString();
        }
    }
}